
from mysql_utils import get_mysql_connection

def log_request(
    request_id: str = None,
    request_type: str = None,
    request_time: int = -1,
    accept_time: int = -1,
    finished_time: int = -1,
    request_body: str = None,  
    ):
    """
    记录请求日志
    """

    if request_type is None:
        request_type = 'default'
    if request_body is None:
        request_body = ''
    network_cost = -1;
    if request_time != -1 and accept_time != -1:
        network_cost = accept_time - request_time;
    process_cost = -1;
    if accept_time != -1 and finished_time != -1:
        process_cost = finished_time - accept_time;

    print ('request_id.class ', type(request_id))
    print ('request_type.class ', type(request_type))
    print ('request_time.class ', type(request_time))
    print ('accept_time.class ', type(accept_time))
    print ('finished_time.class ', type(finished_time))
    print ('request_body.class ', type(request_body))
    mysql_connection = get_mysql_connection()
    with mysql_connection.cursor() as cursor:
        cursor.execute("INSERT INTO t_request_log (request_id, request_type, request_time, accept_time, finished_time, network_cost, process_cost, request_body) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)", 
        (request_id, request_type, request_time, accept_time, finished_time, network_cost, process_cost, request_body))
    mysql_connection.commit()
    mysql_connection.close()
    pass

if __name__ == "__main__":
    log_request(request_id=None, request_type=None, request_time=123, accept_time=179, finished_time=213, request_body="test")